<?php

namespace App\Http\Middleware\Custom;

use App\Utils\JWTHelper\JWTHelper;
use App\Utils\ResponseResult\JsonResult;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class JwtAuthMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response)  $next
     */
    public function handle(Request $request, Closure $next): Response
    {
        //jwt验证
        //1.获取token
        $token=$request->header("Authorization");
        if (!$token){
            return JsonResult::forbidden(null,"token不存在");
        }
        //2.验证token
        if(JWTHelper::verifyToken($token)===false){
            return JsonResult::forbidden(null,"token无效");
        }

        return $next($request);
        //请求出去

    }
}
